 //
//  ADMainTabBar.swift
//  myBlog
//
//  Created by mac2014 on 16/3/20.
//  Copyright © 2016年 AD. All rights reserved.
//

import UIKit

 /// 宏定义
let tabBarItemCount = 5

class ADMainTabBar: UITabBar {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //计算item的
        let width = frame.width / CGFloat(tabBarItemCount)
        
        //计算最左边item的坐标
        let originFrame = CGRect(x: 0, y: 0, width: width, height: frame.height)
        //根据最左边的item进行平移
        var index = 0
        for  view in subviews {
            let classTabbar = NSClassFromString("UITabBarButton")
            if view .isKindOfClass(classTabbar!) {
                view.frame = CGRectOffset(originFrame, width * CGFloat(index), 0)
                index += index == 1 ? 2 : 1
            }
        }
        //计算加号按钮的坐标
        composeBtn.frame = CGRect(x: width * 2, y: 0, width: width, height: frame.height)
        
    }

    /// 懒加载创建加号按钮
    lazy var composeBtn: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(button)

        return button
    }()



}
